home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 46 / Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso / -serious- / programming / other / hexy / src / hexy_fileio.c < prev    next >
C/C++ Source or Header  |  1999-09-06  |  10KB  |  377 lines

  1.  
  2. /*
  3.  * [!BGN - MACHINE GENERATED - DO NOT EDIT THIS HEADER]
  4.  *
  5.  * Program   : Hexy (Binary file viewer/editor for the Amiga.)
  6.  * Version   : 1.6
  7.  * File      : Work:Source/!WIP/HisoftProjects/Hexy/Hexy_fileio.c
  8.  * Author    : Andrew Bell
  9.  * Copyright : Copyright © 1998-1999 Andrew Bell (See GNU GPL)
  10.  * Created   : Saturday 28-Feb-98 16:00:00
  11.  * Modified  : Sunday 22-Aug-99 23:31:45
  12.  * Comment   : 
  13.  *
  14.  * (Generated with StampSource 1.2 by Andrew Bell)
  15.  *
  16.  * [!END - MACHINE GENERATED - DO NOT EDIT THIS HEADER]
  17.  *
  18.  */
  19.  
  20. /* Created: Sun/09/Aug/1998 */
  21.  
  22. /*
  23.  *  Hexy, binary file viewer and editor for the Amiga.
  24.  *  Copyright (C) 1999 Andrew Bell
  25.  *
  26.  *  Author's email address: andrew.ab2000@bigfoot.com
  27.  *
  28.  *  This program is free software; you can redistribute it and/or modify
  29.  *  it under the terms of the GNU General Public License as published by
  30.  *  the Free Software Foundation; either version 2 of the License, or
  31.  *  (at your option) any later version.
  32.  *
  33.  *  This program is distributed in the hope that it will be useful,
  34.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  35.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36.  *  GNU General Public License for more details.
  37.  *
  38.  *  You should have received a copy of the GNU General Public License
  39.  *  along with this program; if not, write to the Free Software
  40.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  41.  *
  42.  */
  43.  
  44. #include <Hexy.h>
  45.  
  46. /* Prototypes */
  47.  
  48. Prototype BOOL ReadFile(UBYTE *FileName, struct VCtrl *VC);
  49. Prototype UBYTE *ObtainInFile( void );
  50. Prototype UBYTE *ObtainOutFile( void );
  51. Prototype void SaveSplit(struct VCtrl *TmpVC);
  52. Prototype void SaveToNewLocation(struct VCtrl *TmpVC);
  53.  
  54. BOOL ReadFile(UBYTE *FileName, struct VCtrl *TmpVC)
  55. {
  56.   /*************************************************
  57.    *
  58.    * Load a file into a VC structure 
  59.    *
  60.    */
  61.  
  62.   BPTR InLock = NULL, InFile = NULL;
  63.   BOOL lresult = TRUE;
  64.  
  65.   Edit_End();
  66.  
  67.   FreeVC(TmpVC);  /* Deallocate any previously loaded file */
  68.  
  69.   if (InLock = Lock(FileName, SHARED_LOCK))
  70.   {
  71.     if (TmpVC->VC_FIB = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL))
  72.     {
  73.       if (Examine(InLock, TmpVC->VC_FIB))
  74.       {
  75.         strcpy( (UBYTE *) &CurFileLoaded, TmpVC->VC_FIB->fib_FileName);
  76.  
  77.         TmpVC->VC_FileLength = TmpVC->VC_FIB->fib_Size;
  78.         TmpVC->VC_CurrentPoint = NULL;    /* Start of file */
  79.         TmpVC->VC_InitialYPos = 24-10;
  80.         TmpVC->VC_YAmount = YLINES;
  81.         TmpVC->VC_XPos = 14-4;
  82.  
  83.         if (TmpVC->VC_FIB->fib_DirEntryType < 0)
  84.         {
  85.           if (TmpVC->VC_FileLength)
  86.           {
  87.             if (TmpVC->VC_FileAddress = (APTR) AllocVec(TmpVC->VC_FileLength, MEMF_ANY))
  88.             {
  89.               if (InFile = Open(FileName, MODE_OLDFILE))
  90.               {
  91.                 stream[0] = (ULONG) &(TmpVC->VC_FIB->fib_FileName);
  92.                 stream[1] = (ULONG) TmpVC->VC_FIB->fib_Size;
  93.                 PrintStatus("Loading file: %.35s (%lu bytes)", &stream);
  94.  
  95.                 if (Read(InFile, TmpVC->VC_FileAddress, TmpVC->VC_FileLength) == TmpVC->VC_FileLength)
  96.                 {
  97.                   UnpackFile(TmpVC);
  98.                   CheckForViruses(TmpVC);
  99.                   SetVDragBar(TmpVC);
  100.                   UpdateView(TmpVC, NULL);
  101.                   DisplayFileInfos(&VC);
  102.                 }
  103.                 else
  104.                 {
  105.                   DoError(FALSE); lresult = FALSE;
  106.                 }
  107.                 Close(InFile);
  108.               }
  109.               else
  110.               {
  111.                 DoError(FALSE); lresult = FALSE;
  112.               }
  113.             }
  114.             else
  115.             {
  116.               DoError(FALSE); lresult = FALSE;
  117.             }
  118.           }
  119.           else
  120.           {
  121.             /* Empty file */
  122.           }
  123.         }
  124.         else
  125.         {
  126.           DoError(FALSE); lresult = FALSE;
  127.         }
  128.       }
  129.       else
  130.       {
  131.         DoError(FALSE); lresult = FALSE;
  132.       }
  133.       /* FreeDosObject(DOS_FIB, VC_FIB); */
  134.     }
  135.     else
  136.     {
  137.       DoError(FALSE); lresult = FALSE;
  138.     }
  139.     UnLock(InLock);
  140.   }
  141.   else
  142.   {
  143.     DoError(FALSE); lresult = FALSE;
  144.   }
  145.  
  146.   if (!lresult) FreeVC(TmpVC);
  147.  
  148.   return lresult;
  149. }
  150.  
  151. UBYTE FullInPath[1024+4];
  152.  
  153. UBYTE *ObtainInFile( void )
  154. {
  155.   /*************************************************
  156.    *
  157.    * Open requester and load a file 
  158.    *
  159.    */
  160.  
  161.   BOOL res = AslRequestTags(FR, ASLFR_Window, MAINWnd,
  162.                   ASLFR_TitleText, "Select input file...",
  163.                   ASLFR_PositiveText, "Load",
  164.                   TAG_DONE);
  165.   if (res)
  166.   {
  167.     if (FR->fr_File[0] == 0)
  168.     {
  169.       PrintStatus("No filename was given!", &stream);
  170.       return NULL;
  171.     }
  172.     strcpy(FullInPath, FR->fr_Drawer);
  173.     if (AddPart( (UBYTE *) &FullInPath, FR->fr_File, 1024))
  174.     {
  175.       return FullInPath;
  176.     }
  177.     else
  178.     {
  179.       PrintStatus("Unable to load selected file: Path is too long!", NULL);
  180.     }
  181.   }
  182.   else
  183.   {
  184.     PrintStatus("Requester aborted!", NULL);
  185.   }
  186.   return NULL;
  187. }
  188.  
  189. UBYTE FullOutPath[1024+4];
  190.  
  191. UBYTE *ObtainOutFile( void )  /* Add FR param */
  192. {
  193.   /*************************************************
  194.    *
  195.    * Open a file requeter that request an output file
  196.    *
  197.    */
  198.  
  199.   UBYTE *Result = (UBYTE *) &FullOutPath;
  200.  
  201.   BOOL res = AslRequestTags(FR, ASLFR_Window, MAINWnd,
  202.                   ASLFR_Flags1, FRF_DOSAVEMODE,
  203.                   ASLFR_TitleText, "Select output file...",
  204.                   ASLFR_PositiveText, "Save",
  205.                   TAG_DONE);
  206.   if (res)
  207.   {
  208.     if (FR->fr_File[0] == 0)
  209.     {
  210.       PrintStatus("No filename was given!", &stream);
  211.       return NULL;
  212.     }
  213.     strcpy(FullOutPath, FR->fr_Drawer);
  214.     if (AddPart( (UBYTE *) &FullOutPath, FR->fr_File, 1024))
  215.     {
  216.       BPTR InLock;
  217.       if (InLock = Lock(FullOutPath, SHARED_LOCK))
  218.       {
  219.         struct FileInfoBlock *FIB;
  220.         if (FIB = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, NULL))
  221.         {
  222.           if (Examine(InLock, FIB))
  223.           {
  224.             if (FIB->fib_Protection & FIBF_DELETE)
  225.             {
  226.               struct EasyStruct ProtectedES =
  227.               {
  228.                 sizeof(struct EasyStruct),
  229.                 NULL,
  230.                 "Hexy info...",
  231.                 "File already exists and is protected from deletetion\nOverwrite it anyway?",
  232.                 "Yes|No"
  233.               };
  234.               if (EasyRequestArgs(MAINWnd, &ProtectedES, NULL, &stream))
  235.               {
  236.                 ULONG PFlags = FIB->fib_Protection & ~FIBF_DELETE;
  237.                 SetProtection(FullOutPath, PFlags);
  238.                 PrintStatus("File overwritten.", NULL);
  239.               }
  240.               else
  241.               {
  242.                 Result = (UBYTE *) NULL;
  243.                 PrintStatus("File was not overwritten.", NULL);
  244.               }
  245.             }
  246.           }
  247.           else DoError(FALSE);
  248.           FreeDosObject(DOS_FIB, FIB);
  249.         }
  250.         else DoError(FALSE);
  251.         UnLock(InLock);
  252.       }
  253.       else DoError(FALSE);
  254.  
  255.       return Result;
  256.     }
  257.     else PrintStatus("Unable to save file: Path is too long!", NULL);
  258.   }
  259.   else PrintStatus("Requester aborted!", NULL);
  260.  
  261.   return NULL;
  262. }
  263.  
  264. ULONG OutLen = NULL;
  265.  
  266. UWORD putchproc[] = { 0x16c0, 0x4e75 };
  267.  
  268. void SaveSplit(struct VCtrl *TmpVC)
  269. {
  270.   /*************************************************
  271.    *
  272.    * Perform a split save 
  273.    *
  274.    */
  275.  
  276.   UBYTE *OutFileName;
  277.  
  278.   if (!ValidFile(TmpVC)) return;
  279.  
  280.   stream[0] = (ULONG) RT_Window;
  281.   stream[1] = (ULONG) MAINWnd;
  282.   stream[2] = (ULONG) RTGL_Min;
  283.   stream[3] = (ULONG) 1;
  284.   stream[4] = (ULONG) RTGL_Max;
  285.   stream[5] = (ULONG) TmpVC->VC_FileLength;
  286.   stream[6] = (ULONG) RTGL_TextFmt;
  287.   stream[7] = (ULONG) "Enter save chunk length...";
  288.   stream[8] = (ULONG) RTGL_Flags;
  289.   stream[9] = (ULONG) GLREQF_CENTERTEXT;
  290.   stream[10] = (ULONG) RT_LockWindow;
  291.   stream[11] = (ULONG) TRUE;
  292.   stream[12] = (ULONG) TAG_DONE;
  293.  
  294.   OutLen = 0; /* 1024*10; */ /* This is temp */
  295.  
  296.   if ( rtGetLongA( (ULONG *) &OutLen, "Split save...", RTFR, (struct TagItem *) &stream) )
  297.   {
  298.     if (OutFileName = ObtainOutFile())
  299.     {
  300.       UBYTE TempPathBuf[1024+4];
  301.       ULONG LoopCnt = NULL, BytesLeft = TmpVC->VC_FileLength;
  302.       UBYTE *SaveAddress = TmpVC->VC_FileAddress;
  303.  
  304.       BPTR OutFile;
  305.  
  306.       if (TmpVC->VC_FileLength < OutLen) OutLen = TmpVC->VC_FileLength;
  307.       do
  308.       {
  309.         stream[0] = (ULONG) OutFileName;
  310.         stream[1] = (ULONG) LoopCnt++;
  311.         RawDoFmt("%.1000s.%04lu", &stream, (void *) &putchproc, &TempPathBuf);
  312.  
  313.         stream[0] = (ULONG) FilePart(TempPathBuf);
  314.         PrintStatus("Saving '%s'... (LMB = Abort)", &stream);
  315.         if (OutFile = Open(TempPathBuf, MODE_NEWFILE))
  316.         {
  317.           if (Write(OutFile, SaveAddress, OutLen) == OutLen)
  318.           {
  319.             SaveAddress += OutLen; Close(OutFile);
  320.           } else DoError(FALSE);
  321.         }
  322.         BytesLeft -= OutLen;
  323.         if (BytesLeft < OutLen) OutLen = BytesLeft;
  324.  
  325.         if (LMBActive()) break;
  326.       }
  327.       while(BytesLeft > 0);
  328.  
  329.       stream[0] = (ULONG) LoopCnt;
  330.       PrintStatus("Saved %lu chunks.", &stream);
  331.     }
  332.   }
  333. }
  334.  
  335. void SaveToNewLocation(struct VCtrl *TmpVC)
  336. {
  337.   /*************************************************
  338.    *
  339.    * Save file to another location 
  340.    *
  341.    */
  342.  
  343.   UBYTE *OutFileName;
  344.  
  345.   if (!ValidFile(TmpVC)) return;
  346.   if (OutFileName = ObtainOutFile())
  347.   {
  348.     BPTR OutFile;
  349.  
  350.     if (OutFile = Open(OutFileName, MODE_NEWFILE))
  351.     {
  352.       if (Write(OutFile, TmpVC->VC_FileAddress, TmpVC->VC_FileLength) != TmpVC->VC_FileLength)
  353.       {
  354.         DoError(FALSE);
  355.       }
  356.       else
  357.       {
  358.         PrintStatus("File was saved OK.", &stream);
  359.       }
  360.       Close(OutFile);
  361.  
  362.       SetProtection(OutFileName, TmpVC->VC_FIB->fib_Protection);
  363.       SetComment(OutFileName, (UBYTE *) &TmpVC->VC_FIB->fib_Comment);
  364.  
  365.     }
  366.     else DoError(FALSE);
  367.   }
  368. }
  369.  
  370. /*************************************************
  371.  *
  372.  * 
  373.  *
  374.  */
  375.  
  376.  
  377.